import plugin from '../../lib/plugins/plugin.js'
import fetch from 'node-fetch'
import cfg from '../../lib/config/config.js'
import {segment} from "oicq";
import sex from "oicq";
import moment from "moment";
var p = ""
var d = ""
var k = ""
var nickname = ""
//随便写的,大佬勿喷 初版@鸢:随机娶群友，指定娶群友
//优化版@尘埃未定:添加我愿意/我拒绝的对象判定，修复bug，66到69行为单次只能主持一场的限制
let whitelist=[216668395]//wife2白名单
const cdTime = 10*60 //wifecd时间,默认为10分钟
export class qqy extends plugin {
	constructor() {
		super({
			name: '娶群友',
			dsc: '娶群友',
			/** https://oicqjs.github.io/oicq/#events */
			event: 'message',
			priority: 0,
			rule: [{
				/** 命令正则匹配 */
                reg: "^#?娶群友$",
				/** 执行方法 */
				fnc: 'Wife'
				},
                {
                /** 命令正则匹配 */
                reg: '^#?娶(.*)$', 
                /** 执行方法 */
                fnc: 'wife2'
                },
                {
                /** 命令正则匹配 */
                reg: '^#?我愿意$', 
                /** 执行方法 */
                fnc: 'yy'
                },
                {
                /** 命令正则匹配 */
                reg: '^#?我拒绝$', 
                /** 执行方法 */
                fnc: 'jj'
                }
            ]
        })
    }
async wife2(e) {
    console.log(e)
    let targetQQ = e.at//获取QQ号
    if(!e.at && !e.atme){
        e.reply("请at你的情人哦")
        return
    }
    if(whitelist.indexOf(targetQQ) > -1) {
        e.reply("这位大人已经名🌸有主了,舔狗！");
        return
    }
    if (e.atme || e.atall){
        e.reply("不可以这样！")
        return
    }
    //注释下面这段就可以抢着娶群友了
    //if(!p== ""){
    //    e.reply(`我正在主持${p}与${k}的婚事。稍等一下哦`)
    //    return
    //}
    let targetName = e.msg.replace(/娶|@|#/g, "").trim();
    nickname=await Bot.pickFriend(e.user_id).nickname
    let sex=await Bot.pickFriend(e.user_id).sex
    let ex=''
    if(sex=='male'){
        ex='小姐'
    }
    else if (sex=='female'){
        ex='先生'
    }
    let  msg = [
        segment.at(targetQQ),"\n",
        `亲爱的${targetName}${ex}您好！`,"\n",
        `在茫茫人海中，能够与${targetName}${ex}相遇相知相恋，${nickname}深感幸福，守护${targetName}是${nickname}今生的选择。${nickname}想有个自己的家，一个有${targetName}的家,嫁给${nickname}好吗？`,"\n",
        segment.image(`https://q1.qlogo.cn/g?b=qq&s=0&nk=${targetQQ}`), "\n",
        segment.image(`https://q1.qlogo.cn/g?b=qq&s=0&nk=${e.user_id}`), "\n",
        `PS:请发送【我愿意】或者【我拒绝】，回应对方哦！`
    ]
    p=e.at
    k=e.user_id
    e.reply(msg)
    return true;
}
async yy(e) {
    d=e.user_id
    if(p==""){
        e.reply("我并没有在主持你和谁的求婚哦")
        return
    }
    if(d == p){
        e.reply('相亲相爱幸福永，同德同心幸福长。愿你俩情比海深！祝福你们新婚愉快，幸福美满，激情永在，白头偕老！')
        p=""
        return
    }
    e.reply(`你不是${p},就不要捣乱了`)
    return true;
}
async jj(e) {
    d=e.user_id
    if(p==""){
        e.reply("我并没有在主持你和谁的求婚哦")
        return
    }
    if(!d == p){
        e.reply(`你不是${p},就不要捣乱了`)
        return
    }
    e.reply('天涯何处无芳草，下次再努力试试吧！')
    p=""
    return true;
}
async Wife(e) {
    const keyword = e.msg.replace(/#?|娶群友/g,"").trim()
    const currentTime = moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
    let lastTime = await redis.get(`potato:whois-my-wife-cd:${e.user_id}`);
    let masterList = cfg.masterQQ
    if (lastTime && !masterList.includes(e.user_id)) {
        const seconds = moment(currentTime).diff(moment(lastTime), 'seconds')
        let tips = [
            segment.at(e.user_id), "\n",
            `等会儿哦！(*/ω＼*)`, "\n",
            `冷却中：${cdTime-seconds}s`
        ]
        e.reply(tips);
        return true;
    }
    let lastSentTime = null
    let sex='female'
    if (await Bot.pickFriend(e.user_id).sex=='female'){
        sex='male'
    }
    let memberMap = await e.group.getMemberMap();
    let arrMember = Array.from(memberMap.values());
    const femaleList=arrMember.filter(item=>{
        return item.sex==sex
    })
    if (femaleList.length<2){
       const unknownList=arrMember.filter(item=>{
           return item.sex=='unknown'
       })
        unknownList.map(item=>{
            femaleList.push(item)
        })

    }
    let flag = false;
    let wife = {}
    console.log(wife);
    for (let i = 0; i < 2; i++) {
        const random = Math.round(Math.random() * (femaleList.length - 1))
        wife = femaleList[random];
            flag = true;
            break;
        
    }
    console.log(wife);
    let msg = []
    if (flag) {
        let sexStr =''
        if(wife.sex=='male'){
            sexStr='男'
        }
        else if (wife.sex=='female'){
            sexStr='女'
        }
        console.log(wife);
        let cp = sexStr
        let rank=await Bot.pickFriend(e.user_id).rank
        let py =''
        if(wife.sex=='male'){
            py='他'
        }
        else if (wife.sex=='female'){
            py='她'
        }
        msg = [
            segment.at(e.user_id),"\n",
            `${wife.nickname}答应了你哦！(*/ω＼*)`,"\n",
            `今天你的${cp}朋友是`, "\n", segment.image(`https://q1.qlogo.cn/g?b=qq&s=0&nk=${wife.user_id}`), "\n",
            `【${wife.nickname}】 (${wife.user_id}) `, "\n",
            `来自【${e.group_name}】`,"\n",
            `要好好对待${py}哦！`,
        ]
        await redis.set(`potato:whois-my-wife-cd:${e.user_id}`, currentTime, {
        EX: cdTime
        });
    }
    else{
        msg = [
            segment.at(e.user_id), "\n",
            `打咩，今天你是单身贵族哦！(*/ω＼*)`
        ]

    }
    e.reply(msg);
    return true;
}
}